-- UICompoundTreasure
-- Create by huanghr
-- 锻造宝物

-- UICompoundTreasure继承自Layer
UICompoundTreasure = class("UICompoundTreasure", function(treasureId)
    return cc.Layer:create();
end);

function UICompoundTreasure.create(treasureId)
    return UICompoundTreasure.new(treasureId);
end

-- 构造函数
function UICompoundTreasure:ctor(treasureId)
    -- 初始化
    self:setName("UICompoundTreasure");
    local node = cc.CSLoader:createNode("layout/risker_equip/CompoundSpell.csb");
    self:addChild(node);
    self.node = node;

    self.treasureId = treasureId;

    self.CT = node:getChildByName("CT");
    self.BT = node:getChildByName("BT");

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 注册事件回调
    self:registerEventCallback();

    -- 适配
    self:resize();
end

-- 注册事件回调
function UICompoundTreasure:registerEventCallback()
    -- 锻造宝物成功事件
    EventMgr.register("UICompoundTreasure", event.FORGE_SUCCESS, function(args)
        UIMgr.getCurrentScene():removeFormByName("UICompoundTreasure");
    end);

    -- 注册获得焦点的回调
    EventMgr.register("UICompoundTreasure", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UICompoundTreasure" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_GEM);
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            EventMgr.removeAll("UICompoundTreasure");
        end
    end);
end

-- 注册点击事件
function UICompoundTreasure:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICompoundTreasure");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 这两个按钮都是直接合成一个
    local btnCenter = findChildByName(self.node, "CT/btn_center");

    -- 锻造六阶宝物
    local function onCompoundClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            --尝试锻造
           self:tryCompound();
        end
    end
    btnCenter:addTouchEventListener(onCompoundClick);

    -- 点击装备，显示装备信息
    local treasureNode = findChildByName(self.CT, "treasure_node");
    treasureNode:setVisible(true);
    local iconImg = findChildByName(treasureNode, "bg");
    local function onEquipClick(sender, eventType)
        -- 点击时的回调
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            require "game/ui/form/equips/UIEquipTooltip";
             -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIEquipTooltip");
            local uiForm = UIEquipTooltip.create(self.treasureId);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end

    iconImg:addTouchEventListener(onEquipClick);
end

-- 重绘
function UICompoundTreasure:redraw()
    -- 绘制标题
    local titleLabel = findChildByName(self.CT, "title_label");
    local subTitle = findChildByName(self.CT, "sub_title");

    local title = ItemM.query(self.treasureId, "name");

    -- 设置标题文本
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(title);

    -- 设置副文本标题
    TextStyleM.setTextStyle(subTitle, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 编辑文本(副标题)，获得消耗
    local cost = FormulaM.invoke("CALC_FORGE_TREASURE_COST", self.treasureId);
    cost = cost[1];
    local quenchId = cost[2];
    local quenchName = ItemM.query(quenchId, "name");
    local hasAmount = ItemM.getAmount(ME.user, quenchId);
    local needAmount = cost[3];

    -- 消耗
    local costText = string.format(getLocStr("compound_spell_cost_rune"), quenchName, hasAmount, needAmount);
    local compoundText = string.format(getLocStr("forge_treasure"), quenchName);

    if hasAmount < needAmount then
        costText = "[red]".. costText .."[-]";
    else
        costText = "[blue]".. costText .."[-]";
    end

    -- 需要图纸
    local dbase = EquipM.query(self.treasureId, "dbase") or {};

    printTable(dbase, "UICompoundTreasure, dbase");
    if dbase["need_drawing"] == 1 and not FormulaM.invoke("CAN_FORGE_SPECIAL_TREASURE") then
        -- 检查当前装备是否激活
        local activedDrawing = DungeonM.getDungeonMixedField("actived_drawing");
        if activedDrawing == nil then
            costText = getLocStr("need_treasure_drawing");
        else
            local arr = string.explode(activedDrawing, "|");

            local value = tostring(self.treasureId);

            if table.indexOf(arr, value) == -1 then
                -- 显示没有图纸
                costText = getLocStr("need_treasure_drawing");
            end
        end
    end

    local subText = compoundText .. costText;

    applyString(subTitle, subText);

    -- 隐藏无关节点
    local materialNode = findChildByName(self.CT, "material_node");
    materialNode:setVisible(false);

    local productNode = findChildByName(self.CT, "product_node");
    productNode:setVisible(false);

    local treasureNode = findChildByName(self.CT, "treasure_node");
    treasureNode:setVisible(true);
    local iconImg = findChildByName(treasureNode, "icon");

    local icon = ItemM.query(self.treasureId, "icon");
    local iconPath = getItemIconPath(icon);
    iconImg:loadTexture(iconPath);

    local arrow = findChildByName(self.CT, "arrow");
    arrow:setVisible(false);

    local starNode = findChildByName(treasureNode, "star_node");
    local treasureRank = ItemM.query(self.treasureId, "rank");
    local layer = createStarLayer(treasureRank, "images/ui/symbol/star_little.png", "center", -0.6);
    if layer then
        layer:setScale(0.75);
        starNode:removeAllChildren();
        starNode:addChild(layer);
    end

    -- 隐藏小的按钮
    local btnLeft = findChildByName(self.node, "CT/btn_left");
    local btnRight = findChildByName(self.node, "CT/btn_right");
    local btnCenter = findChildByName(self.node, "CT/btn_center")

    btnCenter:setVisible(true);
    btnLeft:setVisible(false);
    btnRight:setVisible(false);

    TextStyleM.setTextStyle(btnCenter, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnCenter:setTitleText(getLocStr("btn_text_forge_treasure"));

    if RiskerEquipM.canForgeTreasure(self.treasureId) then
        btnCenter:setBright(true);
        btnCenter:setTouchEnabled(true);
    else
        -- 按钮不能点击
        btnCenter:setBright(false);
        btnCenter:setTouchEnabled(false);
    end
end

-- 锻造宝物
function UICompoundTreasure:tryCompound()
    DungeonActionM.go("forge_treasure", 0, self.treasureId);
end

-- 适配
function UICompoundTreasure:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end